(function (factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD
		define(['jquery'], factory);
	} else if (typeof exports === 'object') {
		// CommonJS
		factory(require('jquery'));
	} else {
		// Browser globals
		factory(jQuery);
	}
}(function ($) {

	var pluses = /\+/g;

	function encode(s) {
		return config.raw ? s : encodeURIComponent(s);
	}

	function decode(s) {
		return config.raw ? s : decodeURIComponent(s);
	}

	function stringifyCookieValue(value) {
		return encode(config.json ? JSON.stringify(value) : String(value));
	}

	function parseCookieValue(s) {
		if (s.indexOf('"') === 0) {
			// This is a quoted cookie as according to RFC2068, unescape...
			s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
		}

		try {
			// Replace server-side written pluses with spaces.
			// If we can't decode the cookie, ignore it, it's unusable.
			// If we can't parse the cookie, ignore it, it's unusable.
			s = decodeURIComponent(s.replace(pluses, ' '));
			return config.json ? JSON.parse(s) : s;
		} catch(e) {}
	}

	function read(s, converter) {
		var value = config.raw ? s : parseCookieValue(s);
		return $.isFunction(converter) ? converter(value) : value;
	}

	var config = $.cookie = function (key, value, options) {

		// Write

		if (value !== undefined && !$.isFunction(value)) {
			options = $.extend({}, config.defaults, options);

			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setTime(+t + days * 864e+5);
			}

			return (document.cookie = [
				encode(key), '=', stringifyCookieValue(value),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path    ? '; path=' + options.path : '',
				options.domain  ? '; domain=' + options.domain : '',
				options.secure  ? '; secure' : ''
			].join(''));
		}

		// Read

		var result = key ? undefined : {};

		// To prevent the for loop in the first place assign an empty array
		// in case there are no cookies at all. Also prevents odd result when
		// calling $.cookie().
		var cookies = document.cookie ? document.cookie.split('; ') : [];

		for (var i = 0, l = cookies.length; i < l; i++) {
			var parts = cookies[i].split('=');
			var name = decode(parts.shift());
			var cookie = parts.join('=');

			if (key && key === name) {
				// If second argument (value) is a function it's a converter...
				result = read(cookie, value);
				break;
			}

			// Prevent storing a cookie that we couldn't decode.
			if (!key && (cookie = read(cookie)) !== undefined) {
				result[name] = cookie;
			}
		}

		return result;
	};

	config.defaults = {};

	$.removeCookie = function (key, options) {
		if ($.cookie(key) === undefined) {
			return false;
		}

		// Must not alter options, thus extending a fresh object...
		$.cookie(key, '', $.extend({}, options, { expires: -1 }));
		return !$.cookie(key);
	};

}));
/*rili*/
var getYearWeek = function (year, month)
{
	var date = new Date (year, month - 1, 1);
	var date2 = new Date (year, 0, 1);
	var day1 = date.getDay ();
	if (day1 == 0)
		day1 = 7;
	var day2 = date2.getDay ();
	if (day2 == 0)
		day2 = 7;
	var d = Math.round ((date.getTime () - date2.getTime () + (day2 - day1) * (24 * 60 * 60 * 1000)) / 86400000);
	return Math.ceil (d / 7) + 1;
};

var setTime = function (timeElement)
{
	var now = new Date ();
};

var createDate = function (date, index)
{
	var solarDiasplay = date.lunarDayName;
	if (date.lunarDay == 1)
	{
		solarDiasplay = date.lunarMonthName;
	}
	
	// 添加节日显示信息
	if (date.term)
	{
		solarDiasplay = date.term;
	}
	if (date.lunarFestival)
	{
		solarDiasplay = date.lunarFestival;
	}
	if (date.solarFestival)
	{
		solarDiasplay = date.solarFestival;
	}
	if (date.lunarHolidays)
	{
		solarDiasplay = date.lunarHolidays;
	}
	if (date.solarHolidays)
	{
		solarDiasplay = date.solarHolidays;
	}
	
	var dateHtml = $ ("<li date=\"" + date.year + "-" + date.month + "-" + date.day + "\" month=\"" + date.month
	        + "\"><div class=\"daywrap\"><div class=\"solarday\">" + date.day + "</div><div class=\"lunarday\">"
	        + solarDiasplay + "</div></div><div class=\"select\"></div></li>");
	if (index % 7 == 0 || index % 7 == 6)
	{
		dateHtml.addClass ("weekend");
	}
	if (date.day - 7 > index || date.day + 7 < index)
	{
		dateHtml.addClass ("othermonth");
	}
	if (date.worktime == 1)
	{
		dateHtml.addClass ("work");
	}
	else if (date.worktime == 2)
	{
		dateHtml.addClass ("rest");
	}
	if (solarDiasplay != date.lunarDayName && solarDiasplay != date.lunarMonthName)
	{
		dateHtml.addClass ("festival");
	}
	return dateHtml.prop ("outerHTML");
};

var setLunarDate = function (year, month, day)
{
	var today = new Date ();
	if (today.getFullYear () == year && today.getMonth () + 1 == month && today.getDate () == day)
	{
		$ (".huangli").addClass ("huanglitoday");
	}
	else
	{
		$ (".huangli").removeClass ("huanglitoday");
	}
	var weekend = LunarCalendar.weekendFromate (year, month, day);
	$ (".huangli .date").html (year + "年" + month + "月"  + "<br/>" + weekend);
	$ (".huangli .bigday").html (day);
	var lunarDate = LunarCalendar.solarToLunar (year, month, day);
	var datestring = LunarCalendar.formateDayD4 (month - 1, day);
	var yi = $ (".huangli .yiji .yi"), ji = $ (".huangli .yiji .ji");
};

var bindDateClick = function ()
{
	$ (".calendarmain .calendarcontent li").each (function (index, value)
	{
		$ (this).click (function ()
		{
			var datestr = $ (value).attr ("date");
			var date = datestr.split ("-");
			setLunarDate (date[0], date[1], date[2]);
			var preSelectElement = $ (".calendarcontent li.selected .select");
			if (preSelectElement.length > 0)
			{
				preSelectElement.width (preSelectElement.width () + 6);
				preSelectElement.height (preSelectElement.height () + 6);
			}
			$ (".calendarcontent li.selected").removeClass ("selected");
			$ (this).addClass ("selected");
			var selectElement = $ (".calendarcontent li.selected .select");
			selectElement.width (selectElement.width () - 6);
			selectElement.height (selectElement.height () - 6);
		});
	});
};

var bindDateHover = function ()
{
	$ (".calendarcontent li.othermonth").hover (function ()
	{
		var month = $ (this).attr ("month");
		$ (".calendarcontent li.othermonth[month='" + month + "']").removeClass ("othermonth").addClass ("hover");
	}, function ()
	{
		$ (".calendarcontent li.hover").removeClass ("hover").addClass ("othermonth");
	});
};

var setToday = function ()
{
	var today = new Date ();
	todayStr = today.getFullYear () + "-" + (today.getMonth () + 1) + "-" + today.getDate ();
	$ (".calendarcontent li[date='" + todayStr + "']").addClass ("today");
};

var setWeekNums = function (year, month)
{
	var week = getYearWeek (year, month);
	var row = $ (".calendarcontent li").length / 7;
	var weekNums = "";
	for ( var i = 0; i < row; i++)
	{
		weekNums += "<li>" + (week + i) + "<br/>周</li>";
	}
	if (row == 5)
	{
		$ (".calendarweeknum ul").removeClass ("sixrow").addClass ("fiverow");
	}
	else
	{
		$ (".calendarweeknum ul").removeClass ("fiverow").addClass ("sixrow");
	}
	$ (".calendarweeknum ul").html (weekNums);
};

var fillCalendar = function (data)
{
	var monthData = data.monthData;
	if ((data.monthDays == 30 && data.firstDay == 6) || (data.monthDays == 31 && data.firstDay >= 5))
	{
		$ (".calendarmain .calendarcontent ul").removeClass ("fiverow").addClass ("sixrow");
	}
	else
	{
		$ (".calendarmain .calendarcontent ul").removeClass ("sixrow").addClass ("fiverow");
		monthData = data.monthData.slice (0, 35);
	}
	var calendarHtml = "";
	for ( var i = 0, length = monthData.length; i < length; i++)
	{
		calendarHtml = calendarHtml.concat (createDate (monthData[i], i));
	}
	$ (".calendarmain .calendarcontent ul").html (calendarHtml);
	bindDateClick ();
	bindDateHover ();
	setToday ();
};

var setCalendar = function (year, month)
{
	$ ("#yeardrop").text (year + "年");
	$ ("#monthdrop").text (month + "月");
	var MonthData = LunarCalendar.calendar (year, month, true);
	fillCalendar (MonthData);
	setWeekNums (year, month);
/*	
	var hlscript = $ ("script[src='Scripts/hl" + year + ".min.js']");
	if (hlscript.length <= 0 && year < 2020 && year > 2008)
	{
		$ ("body").append ("<script src=\"Scripts/hl" + year + ".min.js\"></script>");
	}
*/
	
};

var setYearSelect = function ()
{
	for ( var i = 1891; i <= 2100; i++)
	{
		var yearli = $ ("<li value=\"" + i + "\">" + i + "年</li>");
		$ ("#yearlist ul").append (yearli);
	}
	$ ("#yearlist ul li").click (function ()
	{
		var year = $ (this).attr ("value");
		var month = parseInt ($ ("#monthdrop").text ().replace ("月", ""));
		setCalendar (year, month);
	});
};

var setTheme = function ()
{
	var theme = $.cookie ("theme");
	if (theme)
	{
		$ ("#skin").attr ("href", "styles/" + theme + ".css");
	}
	else
	{
		$ ("#skin").attr ("href", "styles/blue.css");
	}
};

var setFooter = function ()
{
	var screenHeight = $ (window).height ();
	if (screenHeight > 780)
	{
		$ (".footerfloat").removeClass ("footerfloat").addClass ("footer");
	}
};

var init = function ()
{
	var today = new Date ();
	var year = today.getFullYear ();
	var month = today.getMonth () + 1;
	var day = today.getDate ();
	setCalendar (year, month);
	setLunarDate (year, month, day);
	var timeElement = document.getElementById ("time");
	setInterval (function ()
	{
		setTime (timeElement);
	}, 1000);
	setYearSelect ();
};

var loadJs = function (url)
{
	var script = document.createElement ('script');
	script.src = url;
	script.type = 'text/javascript';
	var head = document.head;
	head.appendChild (script);
};

$ (function ()
{  //wrong
	/*document.onselectstart = document.oncontextmenu = document.ondragstart = function ()
	{
		return false;
	};*/
	
	$ ("body>a").hide ();
	setFooter ();
	setTheme ();
	init ();
	$ (".colorpicker a").click (function (event)
	{
		var colorpanel = $ (".colorpanel");
		if (colorpanel.is (":visible"))
		{
			colorpanel.hide ();
		}
		else
		{
			colorpanel.show ();
		}
		if (event.stopPropagation)
		{
			event.stopPropagation ();
		}
		else
		{
			event.cancelBubble = true;
		}
	});//wrong
	$ (".colorpanel li").each (function (index, value)
	{
		$ (value).click (function ()
		{
			var colorselect = "";
			switch (index)
			{
				case 0:
					colorselect = "blue";
					break;
				case 1:
					colorselect = "green";
					break;
				case 2:
					colorselect = "red";
					break;
				case 3:
					colorselect = "orange";
			}
			$.cookie ("theme", colorselect);
			$ ("#skin").attr ("href", "styles/" + colorselect + ".css");
		});
	});


	$ ("#monthlist li").click (function ()
	{
		var month = parseInt ($ (this).html ().replace ("月", ""));
		var year = parseInt ($ ("#yeardrop").text ().replace ("年", ""));
		setCalendar (year, month);
	});
	$ ("#festivallist li").click (function ()
	{
		var date = $ (this).attr ("date");
		var datearr = date.split ("-");
		setCalendar (parseInt (datearr[0]), parseInt (datearr[1]));
		$ ("#holidaydrop").text ($ (this).text ());
		setLunarDate (parseInt (datearr[0]), parseInt (datearr[1]), parseInt (datearr[2]));
	});
	$ ("#monthdecrease").click (function ()
	{
		var year = parseInt ($ ("#yeardrop").text ().replace ("年", ""));
		var month = parseInt ($ ("#monthdrop").text ().replace ("月", ""));
		var date = new Date (year, month - 1);
		date.setMonth (date.getMonth () - 1);
		setCalendar (date.getFullYear (), date.getMonth () + 1);
	});
	$ ("#monthincrease").click (function ()
	{
		var year = parseInt ($ ("#yeardrop").text ().replace ("年", ""));
		var month = parseInt ($ ("#monthdrop").text ().replace ("月", ""));
		var date = new Date (year, month - 1);
		date.setMonth (date.getMonth () + 1);
		setCalendar (date.getFullYear (), date.getMonth () + 1);
	});
	$ ("#today").click (function ()
	{
		var today = new Date ();
		setCalendar (today.getFullYear (), today.getMonth () + 1);
		setLunarDate (today.getFullYear (), today.getMonth () + 1, today.getDate ());
	});
	$ ("body>span").hide ();
});
// JavaScript Document